
#include "util.hpp"
#include "db.hpp"
#include "online.hpp"
#include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"
#include "server.hpp"

void mysql_test()
{
    MYSQL *mysql = mysql_util::mysql_creat("127.0.0.1", "root", "gaoxueloved", "gobang");
    const char *sql = "insert stu value(123,18,'小明');";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if (ret == false)
    {
        return;
    }
    mysql_util::mysql_destroy(mysql);
}

void json_test()
{
    Json::Value root;
    std::string body;
    root["姓名"] = "小明";
    root["年龄"] = 18;
    root["成绩"].append(98);
    root["成绩"].append(68);

    json_util::serialize(root, body);
    DLOG("%s", body.c_str());

    Json::Value val;
    json_util::unserialize(body, val);
    std::cout << "姓名:" << val["姓名"].asString() << std::endl;
    std::cout << "年龄:" << val["年龄"].asInt() << std::endl;
    int sz = val["成绩"].size();
    for (int i = 0; i < sz; i++)
    {
        std::cout << "成绩:" << val["成绩"][i].asInt() << std::endl;
    }
}

void string_test()
{
    std::string src = "123,21,,33,4";
    std::string sep = ",";
    std::vector<std::string> arr;

    string_util::split(src, sep, arr);
    for (auto e : arr)
    {
        DLOG("%s", e.c_str());
    }
}

void file_test()
{
    std::string fliename = "./Makefile";
    std::string body;
    file_util::read(fliename, body);

    std::cout << body;
}

void db_test()
{
    user_table ut("127.0.0.1", "root", "gaoxueloved", "gobang", 3306);
    Json::Value user;
    // user["username"] = "testUser1";
    // user["password"] = "123457";
    // //ut.insert(user);
    // bool ret = ut.login(user);
    // if(ret == false)
    // {
    //     DLOG("登录失败");
    //     return ;
    // }
    // ut.select_by_name("testUser1",user);
    // ut.select_by_id(1,user);
    // std::string tmp;
    // josn_util::serialize(user,tmp);
    // DLOG("%s",tmp.c_str());
    // ut.win(1);
    ut.lose(2);
}

void online_test()
{
    online_manager om;
    websocket_server::connection_ptr conn;
    uint64_t uid = 2;
    om.enter_game_hall(uid, conn);
    if (om.is_in_game_hall(uid))
    {
        DLOG("1、IN GAME HALL");
    }
    else
    {
        DLOG("1、NOT IN GAME HALL");
    }

    om.exit_game_hall(uid);
    if (om.is_in_game_hall(uid))
    {
        DLOG("2、IN GAME HALL");
    }
    else
    {
        DLOG("2、NOT IN GAME HALL");
    }
}

void room_test()
{
    user_table ut("127.0.0.1", "root", "gaoxueloved", "gobang", 3306);
    online_manager om;
    room(10, &ut, &om);
    room_manager room_mager(&ut, &om);
    DLOG("1");
    room_mager.create_room(1, 2);
}

void match_test()
{
    user_table ut("127.0.0.1", "root", "gaoxueloved", "gobang", 3306);
    online_manager om;
    room_manager room_mager(&ut, &om);
    room_ptr rp= room_mager.create_room(1,2);
    matcher mc(&room_mager,&ut,&om);

}

void server_test()
{
    gobang_server _server("127.0.0.1", "root", "gaoxueloved", "gobang", 3306);
    _server.start(8085);
}

int main()
{
    // json_test();
    // string_test();
    // file_test();
    // db_test();
    // online_test();
    // room_test();
    server_test();
    
    return 0;
}
